package pe.gob.sedapal.scmultimedia.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import java.util.ArrayList;
import java.util.List;

import pe.gob.sedapal.scmultimedia.domain.Deuda;
import pe.gob.sedapal.scmultimedia.domain.Suministro;

public class ImprimirUtil implements Printable {

	  //--- Private instances declarations
	  private final double INCH = 72;
	  private Suministro suministro = new Suministro();
	  private List<Deuda> deudas = new ArrayList<Deuda>();
	  private List<String> texto = new ArrayList<String>();

	  public ImprimirUtil(Suministro suministro , List<Deuda> deudas) {
		  
		  this.suministro = suministro;
		  this.deudas = deudas;

		    //--- Create a printerJob object
		    PrinterJob printJob = PrinterJob.getPrinterJob();

		    //--- Set the printable class to this one since we
		    //--- are implementing the Printable interface
		    printJob.setPrintable(this);
		    
		    //--- Show a print dialog to the user. If the user
		    //--- click the print button, then print otherwise
		    //--- cancel the print job
		    
		    if (printJob.printDialog()) {
			      try {
			    	  crearTexto();
			    	  printJob.print();
			      } catch (Exception PrintException) {
			    	  PrintException.printStackTrace();
			    	  System.out.println("ERROR = "+PrintException);
			      }
		    }
	  }
	  
	  public void crearTexto(){
		  String linea = "";
		  texto.add("                                          ");
		  texto.add("       ESTADO DE CUENTA DEL CLIENTE       ");
		  texto.add("******************************************");
		  
		  linea =   "N. de Suministro:                  "+suministro.getNis();
		  texto.add(linea);
		  linea =   "Nombre y Apellido: "+cortarTexto(suministro.getApNombres(), 23);
		  texto.add(linea);
		  linea =   "Direccion: "+cortarTexto(suministro.getDireccion(), 31);
		  texto.add(linea);
		  texto.add("                                          ");
		  texto.add("==========================================");
		  texto.add("  Factura |  Vence   |*MtoFact |*DeudaMes ");
		  texto.add("------------------------------------------");
		  double total = 0;
		  for(Deuda d : deudas){
			  linea = "";
			  
			  linea = linea + DateUtil.dateToString(d.getfFact(), DateUtil.FORMATO_DD_MM_YYYY);
			  linea = linea + "|";
			  linea = linea + DateUtil.dateToString(d.getfVcto(), DateUtil.FORMATO_DD_MM_YYYY);
			  linea = linea + "|";
			  linea = linea + formatearMonto(d.getImpFacturado(),9);
			  linea = linea + "|";
			  linea = linea + formatearMonto(d.getImpDeuda(),10);
			  texto.add(linea);
			  total = total + d.getImpDeuda();
		  }
		  texto.add("------------------------------------------");
		  texto.add("TOTAL ADEUDADO: "+formatearMonto(total, 26));
		  texto.add("==========================================");
		  texto.add("*Montos indicados en nuevos soles.        ");
		  texto.add("                                          ");  
	  }
	  
	  public String formatearMonto(Double monto, int limite){
		  //String cantidad = monto.toString();
		  String cantidad = NumberUtil.formatearNumero(monto);
		  String espacios = "";
		  for(int i=0; i<(limite-cantidad.length()); i++){
			  espacios = espacios +" ";
		  }

		  int tamanio = cantidad.length();
		  if(tamanio > 9){
			  return cantidad.substring(0, limite-1);
		  }else{
			  return espacios.concat(cantidad);
		  }
	  }

	  public String cortarTexto(String texto, int limite){
		  if(limite > texto.length()){
			  
		  }else{
			  if(texto != null){
				  texto = texto.substring(0,limite-1);				  
			  }
		  }
		  return texto;
	  }
	  /**
	   * Method: print
	   * <p>
	   * 
	   * This class is responsible for rendering a page using the provided
	   * parameters. The result will be a grid where each cell will be half an
	   * inch by half an inch.
	   * 
	   * @param g
	   *            a value of type Graphics
	   * @param pageFormat
	   *            a value of type PageFormat
	   * @param page
	   *            a value of type int
	   * @return a value of type int
	   */
	  public int print(Graphics g, PageFormat pageFormat, int page) {
		System.out.println("ENTRO A PRINT");
		    int i;
		    Graphics2D g2d;
		    Line2D.Double line = new Line2D.Double();
		
		    //--- Validate the page number, we only print the first page
		    if (page == 0) {
			
			      //--- Create a graphic2D object a set the default parameters
			      g2d = (Graphics2D) g;
			      g2d.setColor(Color.black);
			
			      //--- Translate the origin to be (0,0)
			      g2d.translate(pageFormat.getImageableX(), pageFormat
			          .getImageableY());
			/*
			      //--- Print the vertical lines
			      for (i = 0; i < pageFormat.getWidth(); i += INCH / 2) {
			        line.setLine(i, 0, i, pageFormat.getHeight());
			        g2d.draw(line);
			      }
			
			      //--- Print the horizontal lines
			      for (i = 0; i < pageFormat.getHeight(); i += INCH / 2) {
			        line.setLine(0, i, pageFormat.getWidth(), i);
			        g2d.draw(line);
			      }
			      */
			      //Print (Text , posX , posY);
			      //g2d.drawString("RKTW*", 0, 10);
			      //
			      Font f = new Font(Font.MONOSPACED, Font.BOLD, 16);
			      g2d.setFont(f);
			      int posY= 10;
			      for(String text : texto){
			    	  g2d.drawString(text, 0, posY);
			    	  posY=posY+15;
			      }
			      /*
			      g2d.drawString("                                          ", 0, 10);
			      g2d.drawString("       ESTADO DE CUENTA DEL CLIENTE       ", 0, 25);
			      g2d.drawString("******************************************", 0, 40);
			      g2d.drawString("N. de Suministro:                  1234567", 0, 55);
			      g2d.drawString("Nombre y Apellido: David Cabanillas Murrug", 0, 70);
			      g2d.drawString("Dirección: Mz C Lt 30 Calle 6 Santa Elisas", 0, 85);
			      g2d.drawString("                                          ", 0, 100);
			      g2d.drawString("==========================================", 0, 115);
			      g2d.drawString("  Factura |  Vence   |*MtoFact |*DeudaMes|", 0, 130);
			      g2d.drawString("------------------------------------------", 0, 145);
			      g2d.drawString("12/12/1290|12/12/1990|11,231.00| 111231.00", 0, 160);
			      g2d.drawString("12/12/1290|12/12/1990|11,231.00| 111231.00", 0, 175);
			      g2d.drawString("------------------------------------------", 0, 190);
			      g2d.drawString("TOTAL ADEUDADO:                 222,462.00", 0, 205);
			      g2d.drawString("==========================================", 0, 220);
			      g2d.drawString("*Montos indicados en nuevos soles.        ", 0, 235);
			      g2d.drawString("                                          ", 0, 250);
			      */
			      return (PAGE_EXISTS);
		    } else{
		    	  return (NO_SUCH_PAGE);
		    }
	  }
	  
	  public static void main(String args[]){
		  //new ImprimirUtil();
	  }
}